package com.cms.recursoshumanos.ui.ocio.reservacion;

import java.util.Date;

import com.cms.recursoshumanos.cache.UserInfoCache;
import com.cms.recursoshumanos.component.base.CustomButtonColored;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.handler.SetReservacionesOcioScreenHandler;
import com.cms.recursoshumanos.object.Ocio;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;

import net.rim.device.api.i18n.SimpleDateFormat;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BorderFactory;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class SetReservacionScreen extends RecursosHumanosScreen
{
    /**
     * Creates a new MyScreen object
     */
	//static Utileria tools = new Utileria();
	private CustomButtonColored boton_set_reservacion;
	private Ocio elOcio;
	
	public Date valor_date_picker;
	private ForegroundManager _foreground;

	private static SetReservacionScreen instance;
	
	TwoColumnFieldManager twoColumnManager;
	
	DateField fecha_reservacion_field, hora_inic_field;
	VerticalFieldManager vfm;
	HorizontalFieldManager hfm_ocio;
	LabelField lbl_nombre_ocio_text, lbl_nombre_ocio_value, lbl_info_solicitud;
	
	
	public static SetReservacionScreen getInstance(Ocio elOcio)
	{
		if(instance == null)
    	{
    		instance = new SetReservacionScreen(elOcio);
    	}
		else
		{
			instance.elOcio = elOcio;
			instance.reset();
		}
    	return instance;
    }
	
	
	public SetReservacionScreen(Ocio elOcio) 
	{
		super( NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
		this.elOcio = elOcio;
		setComponents();
	}
	
	public void setTCMComponents()
	{
		lbl_info_solicitud = new LabelField("Solicitud de Reservacion", FIELD_HCENTER | DrawStyle.HCENTER);
		lbl_info_solicitud.setFont(font_bold_8);
		
		lbl_nombre_ocio_text = new LabelField("Alternativa:", FIELD_LEFT | DrawStyle.HCENTER | DrawStyle.VCENTER);
		lbl_nombre_ocio_value = new LabelField(elOcio.getNombreEstablecimientoOcio(), USE_ALL_WIDTH | DrawStyle.HCENTER);
		
        hfm_ocio = new HorizontalFieldManager(USE_ALL_WIDTH);
		
        hfm_ocio.add(lbl_nombre_ocio_text);
        hfm_ocio.add(lbl_nombre_ocio_value);
		
        Field separator = new SeparatorField();
        separator.setMargin( 6, 0, 6, 0 );
        
        
        SimpleDateFormat formato_hora = new SimpleDateFormat(Constant.DATE_FORMAT_TIME_12_HRS);
        
        fecha_reservacion_field = new DateField("Fecha de Reservacion: ", System.currentTimeMillis(), DateField.DATE);

        hora_inic_field = new DateField("Hora Inicio:", System.currentTimeMillis(), formato_hora, DateField.TIME);
        //hora_fin_field = new DateField("Hora Fin:", System.currentTimeMillis(), formato_hora, DateField.TIME);
        
        	
        
		boton_set_reservacion = new CustomButtonColored("Enviar Solicitud",ButtonField.CONSUME_CLICK | FIELD_HCENTER, Constant.COLOR_BUTTON_FOCUS); 

		boton_set_reservacion.setChangeListener(new FieldChangeListener() 
        {
	   		public void fieldChanged(Field field, int context) 
	       	{ 
	   			 
	   			SetReservacionesOcioScreenHandler handler = new SetReservacionesOcioScreenHandler();
	   			handler.AddReservacionesOcio(
	   						UserInfoCache.getInstance().getUsuario().getNombre_Usuario(),
	   						UserInfoCache.getInstance().getUsuario().getPassword(),
	   						"",
	   						elOcio,
	   						new Date(fecha_reservacion_field.getDate()),
	   						new Date(hora_inic_field.getDate()));
				
	       	} 
		}); 

        twoColumnManager.add(lbl_info_solicitud);
        twoColumnManager.add(separator);
        twoColumnManager.add(hfm_ocio);
        twoColumnManager.add(fecha_reservacion_field);
        twoColumnManager.add(hora_inic_field);
        
        twoColumnManager.add( boton_set_reservacion );
        
        //twoColumnManager.add(hfm);
	}
	private void reset()
	{
		twoColumnManager.deleteAll();
		setTCMComponents();
	}
	private void setComponents() 
	{
		setTitle("Eventos");
		vfm = new VerticalFieldManager( Field.FIELD_HCENTER | VerticalFieldManager.NON_FOCUSABLE  );
		_foreground = new ForegroundManager();
		
		twoColumnManager = new TwoColumnFieldManager( Display.getWidth() / 2, USE_ALL_WIDTH );
        twoColumnManager.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        twoColumnManager.setMargin( 6, 6, 6, 6 );
        twoColumnManager.setPadding( 2, 2, 2, 2 );
		
        this.setTCMComponents();
        
        _foreground.add( twoColumnManager );
        add( _foreground );
        			
	}
	
	
}
